//
//  XJCVideoViewController.m
//  On The Load
//
//  Created by 许嘉成 on 15/11/5.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "XJCVideoViewController.h"
#import "XJCVideoModel.h"
#import "XJCRankViewController.h"
#import "XJCTimeViewController.h"

@interface XJCVideoViewController ()<hideHeaderView,showHeaderView,hideHeaderView1,showHeaderView1>

@property (nonatomic, strong) UIButton *timeBtn;
@property (nonatomic, strong) UIButton *rankBtn;
@property (nonatomic, strong) XJCTimeViewController *timeVC;
@property (nonatomic, strong) XJCRankViewController *rankVC;
@property (nonatomic, strong) UIView *upLine;
@property (nonatomic, strong) UIView *downLine;
@property (nonatomic, strong) UIView *headView;

@property (nonatomic, strong) UIButton *rightBtn;

@end

@implementation XJCVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    // Do any additional setup after loading the view.
//    [self.navigationController.navigationBar setTranslucent:YES];
    [self createViews];
   
}


- (void)createViews{
    //头部分类的块儿
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 40*SCALEH)];
    self.headView.backgroundColor = [UIColor whiteColor];
    self.headView.alpha = 0.8;
    
    self.timeVC = [[XJCTimeViewController alloc]init];
    self.rankVC = [[XJCRankViewController alloc]init];
    [self addChildViewController:self.timeVC];
    [self addChildViewController:self.rankVC];
    [self.view addSubview:self.timeVC.view];
    [self.view addSubview:self.rankVC.view];
    [self.view bringSubviewToFront:self.timeVC.view];
    
//    self.videoTV = [[UITableView alloc]initWithFrame:CGRectMake(0, 64+self.headView.frame.size.height, WIDTH, HEIGHT-64-self.headView.frame.size.height)];
//    self.videoTV.contentSize = CGSizeMake(WIDTH, HEIGHT*2);
    
    [self.view addSubview:self.headView];
    
    self.timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.timeBtn setTitle:@"按时间排序" forState:UIControlStateNormal];
    [self.timeBtn setFrame:CGRectMake(80*SCALEW, 5*SCALEH, 80*SCALEW, 30*SCALEH)];
    self.timeBtn.selected = YES;
    [self.timeBtn setTitleColor:[UIColor colorWithWhite:0.200 alpha:1.000] forState:UIControlStateNormal];
    self.timeBtn.titleLabel.font = [UIFont systemFontOfSize:15 * SCALEH];
    [self.timeBtn addTarget:self action:@selector(timeClick) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:self.timeBtn];
    
    self.upLine = [[UIView alloc]initWithFrame:CGRectMake(80*SCALEW, 4*SCALEH, 80*SCALEW, 1)];
    self.upLine.backgroundColor = [UIColor colorWithWhite:0.600 alpha:1.000];
    [self.headView addSubview:self.upLine];
    
    self.downLine = [[UIView  alloc]initWithFrame:CGRectMake(80*SCALEW, 36*SCALEH, 80*SCALEW, 1)];
    self.downLine.backgroundColor = [UIColor colorWithWhite:0.702 alpha:1.000];
    [self.headView addSubview:self.downLine];
    
    self.rankBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rankBtn setTitle:@"分享排行榜" forState:UIControlStateNormal];
    [self.rankBtn setFrame:CGRectMake(220*SCALEW, 5*SCALEH, 80*SCALEW, 30*SCALEH)];
    self.rankBtn.selected = NO;
    self.rankBtn.titleLabel.font = [UIFont systemFontOfSize:15*SCALEH];
    [self.rankBtn setTitleColor:[UIColor colorWithWhite:0.200 alpha:1.000] forState:UIControlStateNormal];
    [self.rankBtn addTarget:self action:@selector(rankClick) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:self.rankBtn];
    
    //签订隐藏头部视图协议(Time)
    self.timeVC.hideDelegate = self;
    self.timeVC.showDelegate = self;
    //签订隐藏头部视图协议(Rank)
    self.rankVC.hideDelegate1 = self;
    self.rankVC.showDelegate1 = self;
}
#pragma mark -- XJCTimeViewContoller dataSource
- (void)hideHeaderView{
    self.headView.hidden = YES;
}
- (void)showHeaderView{
    self.headView.hidden = NO;
}
#pragma mark -- XJCRankViewContoller dataSource
- (void)hideHeaderView1{
    self.headView.hidden = YES;
}
- (void)showHeaderView1{
    self.headView.hidden = NO;
}

- (void)timeClick{
    self.timeBtn.selected = YES;
    self.rankBtn.selected = NO;
    [self.view bringSubviewToFront:self.timeVC.view];
    [self.view bringSubviewToFront:self.headView];
    [UIView animateWithDuration:0.2 animations:^{
        self.upLine.frame = CGRectMake(80*SCALEW, 4*SCALEH, 80*SCALEW, 1);
        self.downLine.frame = CGRectMake(80*SCALEW, 36*SCALEH, 80*SCALEW, 1);
        
    } completion:^(BOOL finished) {

        //关键帧动画
        
        CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        //创建路径对象
        CGMutablePathRef path = CGPathCreateMutable();
        //设置移动的起始坐标
        CGPathMoveToPoint(path, NULL, self.upLine.center.x, self.upLine.center.y);
        //设置移动经过的点坐标
        CGPathAddLineToPoint(path, NULL, 110 * SCALEW, self.upLine.center.y);
        CGPathAddLineToPoint(path, NULL, 130 * SCALEW, self.upLine.center.y);
        CGPathAddLineToPoint(path, NULL, self.upLine.center.x, self.upLine.center.y);
        
        keyAnimation.duration = 0.5;
        keyAnimation.repeatCount = 1;
        //设置动画轨迹路径
        [keyAnimation setPath:path];
        [self.upLine.layer addAnimation:keyAnimation forKey:@"keyAnimation"];
        
        //关键帧动画
        
        CAKeyframeAnimation *keyAnimation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        //创建路径对象
        CGMutablePathRef path1 = CGPathCreateMutable();
        //设置移动的起始坐标
        CGPathMoveToPoint(path1, NULL, self.downLine.center.x, self.downLine.center.y);
        //设置移动经过的点坐标
        CGPathAddLineToPoint(path1, NULL, 110 * SCALEW, self.downLine.center.y);
        CGPathAddLineToPoint(path1, NULL, 130 * SCALEW, self.downLine.center.y);
        CGPathAddLineToPoint(path1, NULL, self.downLine.center.x, self.downLine.center.y);
        
        keyAnimation.duration = 0.5;
        keyAnimation.repeatCount = 1;
        //设置动画轨迹路径
        [keyAnimation1 setPath:path1];
        [self.downLine.layer addAnimation:keyAnimation1 forKey:@"keyAnimation"];

    }];
    
}
-(void)rankClick{
    self.timeBtn.selected = NO;
    self.rankBtn.selected = YES;
    [self.view bringSubviewToFront:self.rankVC.view];
    [self.view bringSubviewToFront:self.headView];
    [UIView animateWithDuration:0.2 animations:^{
        self.upLine.frame = CGRectMake(220*SCALEW, 4*SCALEH, 80*SCALEW, 1);
        self.downLine.frame = CGRectMake(220*SCALEW, 36*SCALEH, 80*SCALEW, 1);
        
    } completion:^(BOOL finished) {
        
        //关键帧动画
        
        CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        //创建路径对象
        CGMutablePathRef path = CGPathCreateMutable();
        //设置移动的起始坐标
        CGPathMoveToPoint(path, NULL, self.upLine.center.x, self.upLine.center.y);
        //设置移动经过的点坐标
        CGPathAddLineToPoint(path, NULL, 250 * SCALEW, self.upLine.center.y);
        CGPathAddLineToPoint(path, NULL, 270 * SCALEW, self.upLine.center.y);
        CGPathAddLineToPoint(path, NULL, self.upLine.center.x, self.upLine.center.y);
        
        keyAnimation.duration = 0.5;
        keyAnimation.repeatCount = 1;
        //设置动画轨迹路径
        [keyAnimation setPath:path];
        [self.upLine.layer addAnimation:keyAnimation forKey:@"keyAnimation"];
        
        //关键帧动画
        
        CAKeyframeAnimation *keyAnimation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        //创建路径对象
        CGMutablePathRef path1 = CGPathCreateMutable();
        //设置移动的起始坐标
        CGPathMoveToPoint(path1, NULL, self.downLine.center.x, self.downLine.center.y);
        //设置移动经过的点坐标
        CGPathAddLineToPoint(path1, NULL, 250 * SCALEW, self.downLine.center.y);
        CGPathAddLineToPoint(path1, NULL, 270 * SCALEW, self.downLine.center.y);
        CGPathAddLineToPoint(path1, NULL, self.downLine.center.x, self.downLine.center.y);
        
        keyAnimation.duration = 0.5;
        keyAnimation.repeatCount = 1;
        //设置动画轨迹路径
        [keyAnimation1 setPath:path1];
        [self.downLine.layer addAnimation:keyAnimation1 forKey:@"keyAnimation"];

    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
